/**
 * Project Name:lee
 * File Name:UserController.java
 * Package Name:com.lee.controller.system
 * Date:2016年12月12日下午4:25:57
 * Copyright (c) 2016, chenzhou1025@126.com All Rights Reserved.
 *
 */

package com.lee.system.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lee.annotation.SystemLog;
import com.lee.base.controller.BaseController;
import com.lee.common.page.PageView;
import com.lee.common.utils.JsonUtils;
import com.lee.common.utils.POIUtils;
import com.lee.system.model.UserInfo;
import com.lee.system.service.UserService;

/**
 * ClassName:UserController <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2016年12月12日 下午4:25:57 <br/>
 * 
 * @author dell
 * @version
 * @since JDK 1.6
 * @see
 */
@Controller
@RequestMapping("/user/")
public class UserController extends BaseController {
	@Inject
	private UserService userService;

	@RequestMapping("intoUserList")
	public ModelAndView intoUserList() {
		int id = Integer.parseInt(getPara("id"));
		ModelAndView mav = new ModelAndView("/system/user/list");
		mav.addObject("res", findButton(id));
		return mav;
	}

	@ResponseBody
	@RequestMapping("findByPage")
	public PageView findByPage(UserInfo userInfo, String pageNow, String pageSize, String column, String sort) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map = toFormMap(pageNow, pageSize);
		map.put("column", column);
		map.put("sort", sort);
		map.put("userInfo", userInfo);
		List<UserInfo> list = userService.findUserPage(map);
		int count = userService.count(userInfo);
		pageView.setRecords(list);
		pageView.setRowCount(count);
		return pageView;
	}

	@RequestMapping("toAddUser")
	public ModelAndView toAddUser(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/system/user/addUser");
		return mav;
	}
	@RequestMapping("toEditUser")
	public ModelAndView toEditUser(HttpServletRequest request) {
		String id = this.getPara("id");
		UserInfo userInfo = userService.findById(Integer.parseInt(id));
		ModelAndView mav = new ModelAndView("/system/user/editUser");
		mav.addObject("userInfo", userInfo);
		return mav;
	}
	
	@SystemLog(module="系统管理",methods="添加用户")
	@RequestMapping("addUser")
	@ResponseBody
	public String addUser(HttpServletRequest request, UserInfo userInfo) {
		String flag = "false";
		int i = userService.insert(userInfo);
		if (i > 0) {
			flag = "true";
		}
		return flag;
	}
	
	@SystemLog(module="系统管理",methods="修改用户")
	@RequestMapping("editUser")
	@ResponseBody
	public String editUser(HttpServletRequest request, UserInfo userInfo) {
		String flag = "false";
		int i = userService.update(userInfo);
		if (i > 0) {
			flag = "true";
		}
		return flag;
	}
	
	@SystemLog(module="系统管理",methods="删除用户")
	@RequestMapping("delUser")
	@ResponseBody
	public String delUser(HttpServletRequest request, int id) {
		String flag = "false";
		int i = userService.delete(id, false);
		if (i > 0) {
			flag = "true";
		}
		return flag;
	}
	/**
	 * 
	 * delMore:(批量删除). <br/>
	 * TODO(这里描述这个方法适用条件 – 可选).<br/>
	 * TODO(这里描述这个方法的执行流程 – 可选).<br/>
	 * TODO(这里描述这个方法的使用方法 – 可选).<br/>
	 * TODO(这里描述这个方法的注意事项 – 可选).<br/>
	 *
	 * @author dell
	 * @param request
	 * @return
	 * @since JDK 1.7
	 */
	@SystemLog(module="系统管理",methods="批量删除用户")
	@RequestMapping("delMore")
	@ResponseBody
	public Map<String, Object> delMore(HttpServletRequest request) {
		String[] ids = getParaValues("id");
		Map<String, Object> map = new HashMap<String, Object>();
		// true代表真删除，false代表假删除
		int count = 0;
		try {
			int i = 0;
			for (String id : ids) {
				i = userService.delete(Integer.parseInt(id), true);
				if (i > 0) {
					count++;
				}
			}
		} catch (Exception e) {
			e.getStackTrace();
		}
		map.put("sucCount", count);
		map.put("faiCount", (ids.length - count));
		return map;
	}
	
	//@SystemLog(module="系统管理",methods="导出用户列表")
	@RequestMapping("export")
	@ResponseBody
	public void export(UserInfo userInfo,HttpServletRequest request, HttpServletResponse response) throws IOException {
		String fileName = "用户列表";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userInfo", userInfo);
		String exportData = this.getPara("exportData");// 列表头的json字符串

		List<Map<String, Object>> listMap = JsonUtils.parseJSONList(exportData);

		List<UserInfo> lis = userService.findUserByCount(map);
		POIUtils.exportToExcel(response, listMap, lis, fileName);
	}
}
